from services.server_integration.api import ServerIntegrationApi
from services.new_services.schemas.cviya_config import Block, BLOCKS
from services.new_services.new_server_integration.cviya import _get_config_by_cviya
from services.new_services.new_server_integration.api import NewServerIntegrationApi
from services.new_services.new_server_integration.schemas.common_configuration import BlockType
from services.new_services.new_server_integration.schemas.response_message import ResponseMessage
from services.new_services.technic_page.schemas.technic_configuration import TechnicConfiguration
from services.new_services.new_server_integration.schemas.channels_configuration import ConnectionType
from services.new_services.new_server_integration.schemas.full_unit_configuration import NewFullUnitConfiguration


class TechnicPage:

    SI_API = ServerIntegrationApi()
    NEW_SI_API = NewServerIntegrationApi()

    def save_new_only_serial_mac_and_other(self, current_new_full_unit_config: NewFullUnitConfiguration, technic_configuration: TechnicConfiguration) -> NewFullUnitConfiguration:
        current_new_full_unit_config.common_configuration.block_config.block_info.serial_number = technic_configuration.serial_number
        current_new_full_unit_config.common_configuration.block_config.block_info.mac_address = technic_configuration.mac_address

        # ТУТ АВТО ГЕЙН ЭТО TX И RX ВМЕСТЕ, ТО ЕСТЬ ДВА ПОЛЯ ИСПОЛЬЗУЮТСЯ. Я БЕРУ ЕГО ЗНАЧЕНИЕ ПО TX
        current_new_full_unit_config.channels_configuration.auto_gain_enabled = technic_configuration.ac_link_tx

        return current_new_full_unit_config

    def save_to_new_full_unit_config(self, current_new_full_unit_config, technic_configuration: TechnicConfiguration, block_config_by_cviya: Block, block_type_by_cviya: str) -> NewFullUnitConfiguration:
        if technic_configuration.cviya != current_new_full_unit_config.common_configuration.block_cviya:

            for index in range(2):
                current_new_full_unit_config.channels_configuration.relay_channels[index].enabled = None
                current_new_full_unit_config.channels_configuration.relay_channels[index].connection_type = ConnectionType.EMPTY

            for index in range(4):
                current_new_full_unit_config.channels_configuration.service_channels[index].enabled = block_config_by_cviya.sms_enabled[index]
                current_new_full_unit_config.channels_configuration.service_channels[index].frequency = block_config_by_cviya.sms_freq[index]
                current_new_full_unit_config.channels_configuration.service_channels[index].editable = block_config_by_cviya.sms_editable[index]

            current_new_full_unit_config.common_configuration.block_type = block_type_by_cviya
            current_new_full_unit_config.common_configuration.block_cviya = technic_configuration.cviya

        current_new_full_unit_config = self.save_new_only_serial_mac_and_other(current_new_full_unit_config, technic_configuration)

        return current_new_full_unit_config

    async def set_unit_config_by_technic(self, technic_configuration: TechnicConfiguration, user_role: str) -> bool | ResponseMessage:
        current_new_full_unit_config = NewFullUnitConfiguration(**(await self.NEW_SI_API.get_new_full_configuration()))

        await self._set_automat_config(technic_configuration, user_role)

        # ВОЗВРАЩАЮ ОБЪЕКТ - БЛОК, КОТОРЫЙ СОДЕРЖИТ НУЖНЫЕ НАСТРОЙКИ СМ И РМ
        technic_cviya: str = technic_configuration.cviya
        _fsr_or_fsc_by_cviya = lambda x: BlockType.FSR if technic_cviya in [block.cviya for block in BLOCKS if block.block_type == BlockType.FSR] else BlockType.FSC

        block_config_by_cviya: Block | ResponseMessage = _get_config_by_cviya(technic_cviya)
        block_type_by_cviya: str = _fsr_or_fsc_by_cviya(technic_cviya)

        if type(block_config_by_cviya) is ResponseMessage:
            return block_config_by_cviya

        current_new_full_unit_config = self.save_to_new_full_unit_config(current_new_full_unit_config,
                                                                         technic_configuration, block_config_by_cviya,
                                                                         block_type_by_cviya)

        await self.NEW_SI_API.set_new_full_configuration(current_new_full_unit_config, user_role)

        return True

    async def _set_automat_config(self, technic_configuration: TechnicConfiguration, user_role: str):
        armyanskii_config = await self.SI_API.get_full_configuration()

        armyanskii_config.unit_configuration.ac_link_rx = technic_configuration.ac_link_rx
        armyanskii_config.unit_configuration.ac_link_tx = technic_configuration.ac_link_tx

        armyanskii_config.unit_configuration.ac_service_tx = technic_configuration.ac_service_tx

        armyanskii_config.unit_configuration.acc_base_power_calibrate = technic_configuration.acc_base_power_calibrate
        armyanskii_config.unit_configuration.acc_link_calibrate = technic_configuration.acc_link_calibrate
        armyanskii_config.unit_configuration.acc_tx_service_calibrate = technic_configuration.acc_tx_service_calibrate

        await self.SI_API.set_full_configuration(armyanskii_config, user_role)
